package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.RegisterCMD;
import com.glowdayz.glowmee.dao.RegisterDAO;

public class ListAdminBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListAdminBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String id_str = request.getParameter("id");
		String isActivity_str = request.getParameter("isActivity");
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		if (order != null && order.equals("isUnActivity")) {
			order = "isActivity ASC, create_date DESC";
			request.setAttribute("order", "&order=isUnActivity");
		} else if (order != null && order.equals("isActivity")) {
			order = "isActivity DESC, create_date DESC";
			request.setAttribute("order", "&order=isActivity");
		}
		
		RegisterDAO dao = RegisterDAO.getInstance();
		
		if (id_str != null && isActivity_str != null) {
			int id = Integer.parseInt(id_str);
			int isActivity = Integer.parseInt(isActivity_str);
			
			if (isActivity == 1) {
				isActivity = 0;
			} else if (isActivity == 0) {
				isActivity = 1;
			}
			
			RegisterCMD cmd = new RegisterCMD(id, isActivity);
			try {
				dao.unActivityRegister(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("register", dao.getRegisterList(page, 10, "ADMIN", order, null));
			
			request.setAttribute("activity", DBHandler.getInstance().getCount("register", "idRegister", "userType='ADMIN' AND isActivity='1'"));
			request.setAttribute("unActivity", DBHandler.getInstance().getCount("register", "idRegister", "userType='ADMIN' AND isActivity='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listAdmin.jsp");
		dpr.forward(request, response);
	}

	/** 어드민 계정추가로 처리되는 구간 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String nickName = request.getParameter("nickName");
		String eMail = request.getParameter("eMail");
		String passWord1 = request.getParameter("passWord1");
		int grade = Integer.parseInt(request.getParameter("grade"));
		
		try {
			RegisterCMD cmd = new RegisterCMD(eMail, passWord1, nickName, grade);
			RegisterDAO.getInstance().insertAdmin(cmd);
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		try {
			RegisterDAO dao = RegisterDAO.getInstance();
			
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("register", dao.getRegisterList(1, 10, "ADMIN", null, null));
			
			request.setAttribute("activity", DBHandler.getInstance().getCount("register", "idRegister", "userType='ADMIN' AND isActivity='1'"));
			request.setAttribute("unActivity", DBHandler.getInstance().getCount("register", "idRegister", "userType='ADMIN' AND isActivity='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listAdmin.jsp");
		dpr.forward(request, response);
	}

}